package com.gzhryc.shared_device.common.payment.services;

import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.JdbcSession;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.common.payment.dao.AlipayConfigDao;
import com.gzhryc.shared_device.common.payment.dao.db.AlipayConfig;
import com.gzhryc.shared_device.common.payment.dao.enums.EAppType;
import com.gzhryc.shared_device.common.payment.services.dto.AlipayConfigSearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class AlipayConfigService {

    static Logger log = Logger.getLogger(AlipayConfigService.class);

    public static AlipayConfigService self() {
        return MultiDBTools.getService(AlipayConfigService.class, true);
    }

    public static AlipayConfigService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, AlipayConfigService.class, true);
    }

    AlipayConfigDao dao;

    public AlipayConfigService(String jdbcKey) {
        this.dao = new AlipayConfigDao(jdbcKey);
    }

    public List<AlipayConfig> findAll(){
        try {
            return dao.findAll();
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public List<AlipayConfig> findJoinBySearch(AlipayConfigSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        conditions.desc("createDate");
        try {
            return dao.findJoinByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countJoinBySearch(AlipayConfigSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.countJoin(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public Long countByAccountGroupId(Long accountGroupId){
        if(accountGroupId != null){
            Conditions conditions = new Conditions(AlipayConfig.class);
            conditions.and().is("accountGroupId",accountGroupId);

            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public AlipayConfig getByAppId(String appId){
        try {
            return dao.getById(appId);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public AlipayConfig getJoinByAppId(String appId){
        try {
            return dao.getJoinById(appId);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(AlipayConfig alipayConfig) throws LogicException {
        alipayConfig.setCreateDate(new Date());
        try {
            return dao.insert(alipayConfig) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(AlipayConfig alipayConfig) throws LogicException{
        try {
            if(dao.updateNotNull(alipayConfig) > 0){
                return true;
            }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteByAppId(String appId) throws LogicException{
        JdbcSession.begin();
        try {
            if(dao.deleteById(appId) > 0){
                if(AppConfigService.self().deleteByKeys(appId, EAppType.Aliyun.index())){
                    JdbcSession.commit();
                    return true;
                }
            }
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }finally {
            JdbcSession.end();
        }
        return false;
    }
}
